package cn.tedu.sims.controller;

import cn.tedu.sims.pojo.dto.*;
import cn.tedu.sims.pojo.entity.Project;
import cn.tedu.sims.pojo.vo.*;
import cn.tedu.sims.service.IProjectService;
import cn.tedu.sims.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "项目申报")
@Validated
@RestController
@RequestMapping("/projects")
public class ProjectController {
    @Autowired
    private IProjectService service;
    @ApiOperation("申报")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/projects/add-new')")
    @PostMapping("/add-new")
    public JsonResult<Void> insertProject(ProjectAndProjectInformationDTO projectAndProjectInformationDTO){
        log.debug("添加项目");
        log.debug("接收到添加项目请求:{}",projectAndProjectInformationDTO);
        service.addNew(projectAndProjectInformationDTO);
        return JsonResult.ok();
    }

    @ApiOperation("项目列表")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/projects/projectList/reportType')")
    @GetMapping("/projectList/{reportType}")
    public JsonResult<List<ProjectVO>> ProjectList(@PathVariable String reportType){
        log.debug("项目列表");
        List<ProjectVO> list = service.projectList(reportType);
        return JsonResult.ok(list);
    }

    @ApiOperation("删除项目")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "项目id", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/projects/id/delete')")
    @PostMapping("/{id}/delete")
    public JsonResult<Void> delete(@Range(min=1,message = "删除项目失败," +
            "尝试删除的相册ID无效!")@PathVariable long id) {
        log.debug("开始处理[删除项目]id={}",id);
        service.deleteById(id);
        return JsonResult.ok();
    }
    @ApiOperation("批量删除项目")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('/projects/deleteByIds/ids')")
    @PostMapping("/deleteByIds/{ids}")
    public JsonResult<Void> delete(@PathVariable Long[] ids) {
        log.debug("开始处理[批量删除项目]id=[]",ids);
        service.deleteByIds(ids);
        return JsonResult.ok();
    }
    @ApiOperation("项目查找")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/projects/selectByYear')")
    @PostMapping("/selectByYear")
    public JsonResult<List<ProjectVO>> selectByYear(ProjectYearDTO project){

        log.debug("{}",project);
        List<ProjectVO> projectVOS = service.selectByYear(project);
        log.debug("project:{}",project);
        return JsonResult.ok(projectVOS);
    }
    @ApiOperation("首页审核项目查找")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/projects/selectByAudit')")
    @GetMapping("/selectByAudit")
    public JsonResult<List<ProjectVO>> selectByAudit(){

        List<ProjectVO> projectVOS = service.selectByAudit();
        return JsonResult.ok(projectVOS);
    }
    @ApiOperation("通过id查找")
    @ApiOperationSupport(order = 402)
    @PreAuthorize("hasAuthority('/projects/selectById/id')")
    @GetMapping("/selectById/{id}")
    public JsonResult<ProjectVO> selectById(@PathVariable Long id){
        ProjectVO projectVO = service.selectById(id);
        return JsonResult.ok(projectVO);
    }
    @ApiOperation("项目审核通过")
    @ApiOperationSupport(order = 500)
    @PreAuthorize("hasAuthority('/projects/stateOkById/id')")
    @PostMapping("/stateOkById/{id}")
    public JsonResult<Void> ok(@PathVariable long id){
        service.stateOkById(id);
        return JsonResult.ok();
    }

    @ApiOperation("详情")
    @ApiOperationSupport(order = 510)
    @PreAuthorize("hasAuthority('/projects/projectInformation/projectId')")
    @GetMapping("/projectInformation/{projectId}")
    public JsonResult<ProjectInformationVO> ProjectInformation(@PathVariable long projectId){
        log.debug("通过id{}查询详情",projectId);
        ProjectInformationVO projectInformationVO=service.getProjectInformationVOByProjectId(projectId);
        return JsonResult.ok(projectInformationVO);
    }


    @ApiOperation("回驳")
    @ApiOperationSupport(order = 600)
    @PreAuthorize("hasAuthority('/projects/add-new-projectReject')")
    @PostMapping("/add-new-projectReject")
    public JsonResult<Void> addNewProjectReject(ProjectRejectDTO projectRejectDTO){
        log.debug("接收到添加回驳项目请求:{}",projectRejectDTO);
        service.addNewProjectReject(projectRejectDTO);

        return JsonResult.ok();
    }
    @ApiOperation("获取回驳")
    @ApiOperationSupport(order = 601)
    @PreAuthorize("hasAuthority('/projects/getRejectMessage/id')")
    @GetMapping("/getRejectMessage/{id}")
    public JsonResult<String> getRejectMessage(@PathVariable int id){
        log.debug("接收到获取回驳项目请求:参数{}",id);
        String rejectMessageByProjectId = service.getRejectMessageByProjectId(id);
        return JsonResult.ok(rejectMessageByProjectId);
    }

    @ApiOperation("修改")
    @ApiOperationSupport(order = 700)
    @PreAuthorize("hasAuthority('/projects/update')")
    @PostMapping("/update")
    public JsonResult<Void> update(Project project){
        log.debug("接收到添加回驳项目请求:{}",project);
        service.update(project);
        return JsonResult.ok();
    }

    @ApiOperation("查找项目类别的数量")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/projects/selectCountByType')")
    @GetMapping("/selectCountByType")
    public JsonResult<ProjectReportTypeVO> selectCountByType(){
        ProjectReportTypeVO projectReportTypeVO=service.selectCountByType();
        log.info("请求获取到的参数为：{}",projectReportTypeVO);
        return JsonResult.ok(projectReportTypeVO);
    }
    @ApiOperation("查找项目的通过率")
    @ApiOperationSupport(order = 410)
    @PreAuthorize("hasAuthority('/projects/selectPass')")
    @GetMapping("/selectPass")
    public JsonResult<String[]> selectPass(){

        log.debug("项目通过率：{}",service.selectPass());
        return JsonResult.ok(service.selectPass());
    }



}
